<?php


class GroupsController extends AppController{

     var $name='Groups';
     var $uses = array("Attachment","Group","User");     
     var $helpers=array('Pagination','Html','Javascript','Form','Fck','Str','Action');
     var $components = array ('Pagination');
     var $layout='hindex';
    
    //首页内容
    function index() 
    {
        //$this->Group->findAll();
        $pagesize=10;
        $paging['show']=$pagesize;
        $paging['url'] = '/groups/index/';
        $urlcount=0;
        
        $where="";
        $where.="(Group.is_deleted='0')";
        $urlcount=$this->Group->findCount($where);
        $paging['total'] =$urlcount;
        $showpage=0;
        $users=array();
        if ($paging['total'] > 0) {
            list ($order, $limit, $page) = $this->Pagination->init("", $paging);
            //$this->Group->unbindModel(array ('hasMany'));
            $groups=$this->Group->findAll($where,
            "`Group`.`id`, `Group`.`group_name`, `Group`.`owner`,`Group`.`remark`,`Group`.`created`,`Group`.`modified`
            ",
            "Group.created desc",$limit,$page);
            if($urlcount>$pagesize)
            {
                $showpage=1;
            }
        }
        
        //$groups=$this->Group->findAll();
        $this->set(compact('groups','showpage'));
    }

    function view($id) {
        $photourls = array();
        $this->Group->id = $id;
        $group =  $this->Group->read();
       
        $this->set('group', $group);
        if ($group['User']){
            foreach ($group['User'] as $g){
                $s = $g['photo_id'];
                if($s){
                $photourls[$g['id']] = $this->Attachment->findById($s);
                }
            }
        }
        
        $inmembers =$this->User->findAll("User.groupid ='$id' and User.is_deleted='0' and cid is null",null,"User.modified desc");
        
        //$notmembers =$this->User->findAll("(User.groupid !='$id' or User.groupid is null) and User.is_deleted='0' and cid is null",null,"User.modified desc");
        // var_dump($inmembers);die;
       
        $imuser=array();
        if ($notmembers){
                if (is_array($notmembers)){
                    foreach ($notmembers as $not){
                       // var_dump(($not['User']['groupid']));exit;
                        //$imuser[$not['User']['id']] = $not['User'];
                        if (empty($not['User']['groupid']) && $not['User']['is_deleted'] == '0' && empty($not['User']['cid'])){
                             $imuser[$not['User']['id']] = $not['User'];
                        }
                    }
                }else{
                    if (empty($not['User']['groupid'])&& empty($not['User']['cid'])){
                        $imuser[$not['User']['id']] = $not['User'];
                    }
                }			
            }
        //    var_dump($notmembers);exit;
          //  var_dump($imuser);exit;
           //var_dump($imuser);exit;
        $this->set(compact('photourls','inmembers'));
        $this->set(compact('imuser','id'));	
    }
    function add() {
        if (!empty($this->data)) {
            if ($this->Group->save($this->data)) {
                $this->Session->setFlash('部门已保存');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

    //添加成员
    function addusers() {
        $groupid = $_POST['groupid'];
        $userids = $_POST['immembers'];
        if ($groupid && $userids){
            foreach ($userids as $userid){
                if ($userid){
                    $nuser = $this->User->findById($userid);
                    $nuser["User"]["groupid"]=$groupid;
                    $this->User->save($nuser);
//					$this->data["GroupUser"]["user_id"]=$userid;
//					$this->data["GroupUser"]["group_id"]=$groupid;
//					$this->GroupUser->save($this->data);
//					$this->GroupUser->id=null;
                }
            }
        }
         $this->Session->setFlash('用户添加成功');
        $this->redirect(array('action'=>'view',$groupid));
    }
    
  //移除成员
  function deleteuser($id){
    $nuser = $this->User->findById($id);
    $groupid = $nuser["User"]["groupid"];
    $nuser["User"]["groupid"]=null;
    $nuser["User"]["isgroupadmin"]=0;
    
    $this->User->save($nuser);
//    $conditions = array("GroupUser.group_id" => $groupid,'GroupUser.user_id' =>$userid );
//    //使用模型查找的例子
//    $cn = $this->GroupUser->find($conditions);
//  	$this->GroupUser->del($cn['GroupUser']['id']);

     $this->Session->setFlash('操作成功');
    $this->redirect(array('action'=>'view',$groupid));
  }
  //设置管理员
  function makeadmin($id){
    $nuser = $this->User->findById($id);
    $nuser["User"]["isgroupadmin"]=1;
    $groupid = $nuser["User"]["groupid"];
    $this->User->save($nuser);
    $this->Session->setFlash('操作成功');
    $this->redirect(array('action'=>'view',$groupid));
    //echo 1;
  }
  //移除管理员
  function removeadmin($id){
    //$groupid = UrlDecode($_GET['groupid']);
    $nuser = $this->User->findById($id);
    $nuser["User"]["isgroupadmin"]=0;
    $groupid = $nuser["User"]["groupid"];
    $this->User->save($nuser);
    $this->Session->setFlash('操作成功');
    $this->redirect(array('action'=>'view',$groupid));
  }
    //编辑部门
    function edit($id = null) {
       $this->Group->id = $id;
            if (empty($this->data)) {
                 $this->data = $this->Group->read();
            } else {
                if ($this->Group->save($this->data)) {
                     $this->Session->setFlash('部门信息修改成功.');
                     $this->redirect(array('action' => 'index'));
            }
        }
        $this->set(compact('id'));
    }
    //删除部门
    function deletegroup($groupid){
        $group=$this->Group->findById($groupid);
        if($group["User"]){
            foreach ($group['User'] as $u){
                $user=$this->User->findById($u['id']);
                $user['User']['groupid'] = null;
                $user["User"]["isgroupadmin"]=0;
                $this->User->save($user);
                $this->User->id=null;
            }
        }
        $group["Group"]["is_deleted"]=1;
        $this->Group->save($group);
        $this->Session->setFlash('部门删除成功.');
        $this->redirect(array('action'=>'index'));
    }
    //中文
    function chn()
    {
        $language = 'chn';
        $this->Session->write('Config.language', $language);
        $this->redirect($this->referer());
    }
    //英文
    function eng()
    {
        $language = 'eng';
        $this->Session->write('Config.language', $language);
        $this->redirect($this->referer());
    }
    //打开页面前执行的方法
    function beforeFilter()
    {
        parent::beforeFilter();
        $cuser=$this->Session->read('User');
        if (!$cuser){
            $this->redirect('/users/login');
        }
    }
    //打开页面后执行的方法
    function afterFilter()
    {
        
    }
}
?>